package com.yzd.learnjava.io;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import com.yzd.learnjava.util.Print;

public class Blip3 implements Externalizable{
	private int i ;
	private String s ;
	public Blip3() {
		Print.print("Blip3 Constructor");
	}
	
	public Blip3(int i , String x) {
		Print.print("Blip3(int i , String x)");
		this.i = i ;
		this.s = x;
	}
	
	public String toString() {
		return s + i;
	}
	
	public void writeExternal(ObjectOutput out) throws IOException {
		Print.print("Blip3, writeExternal" );
		out.writeObject(s);
		out.writeInt(i);
	}
	
	public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException {
		Print.print("Blip3, readExternal");
		s = (String)in.readObject();
		i = in.readInt();
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Print.print("Constructing objects");
		Blip3 b3 = new Blip3(47, "A string");
		Print.print(b3);
		
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blip3.out"));
		Print.print("saving object");
		o.writeObject(b3);
		o.close();
		
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blip3.out"));
		Print.print("Recobering b3");
		b3 = (Blip3)in.readObject();
		Print.print(b3);
		
		
	}
	
}




















